package com.example.demo;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.FlatMapOperator;
import org.apache.flink.api.java.operators.UnsortedGrouping;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.optimizer.operators.FlatMapDescriptor;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * Flink demo
 */
public class BatchWordCount {
//    public static void main(String[] args) throws Exception {
//        //创建执行环境
//        ExecutionEnvironment executionEnvironment = ExecutionEnvironment.getExecutionEnvironment();
//        //从文件中读取文件
//        DataSource<String> stringDataStreamSource = executionEnvironment.readTextFile("demo.txt");
//        //输出 生成二元组数据
//        FlatMapOperator<String, Tuple2<String, Long>> returns = stringDataStreamSource.flatMap((String line, Collector<Tuple2<String, Long>> out) -> {
//            //对每一行文本进行拆分
//            String[] words = line.split(" ");
//            for (String word : words) {
//                out.collect(Tuple2.of(word, 1L));
//            }
//        }).returns(Types.TUPLE(Types.STRING, Types.LONG));
//        //按照 word进行分组
//        UnsortedGrouping<Tuple2<String, Long>> tuple2UnsortedGrouping = returns.groupBy(0);
//        //累加
//        AggregateOperator<Tuple2<String, Long>> sum = tuple2UnsortedGrouping.sum(1);
//        //结果
//        sum.print();
//    }
}
